﻿/*-------------------------------------------------------------------------
* 版权所有：JoneZhu
* 作者：朱宏达
* 联系方式：zhuhongda@gyyx.cn
* 创建时间：2014-07-15 18:20:14
* 版本号：v1.0
* 本类主要用途描述：
* Table的帮助类
-------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchemaExplorer;

public class TableObj
{
    private readonly TableSchema _tableSchema;
    private readonly string _prefix;
    private readonly string _suffix;

    /// <summary>
    /// 所有列的集合
    /// </summary>
    private readonly List<ColumnObj> _columnList = new List<ColumnObj>();

    public List<ColumnObj> ColumnList
    {
        get { return _columnList; }
    }

    /// <summary>
    /// 主键集合
    /// </summary>
    private readonly List<ColumnObj> _pkColumnList = new List<ColumnObj>();

    public List<ColumnObj> PkColumnList
    {
        get { return _pkColumnList; }
    }

    /// <summary>
    /// 非主键列集合
    /// </summary>
    private readonly List<ColumnObj> _unPkcolumnList = new List<ColumnObj>();

    public List<ColumnObj> UnPkcolumnList
    {
        get { return _unPkcolumnList; }
    }

    /// <summary>
    /// 自增集合
    /// </summary>
    private readonly List<ColumnObj> _increColumnList = new List<ColumnObj>();

    public List<ColumnObj> IncreColumnList
    {
        get { return _increColumnList; }
    }

    /// <summary>
    /// 非自增集合
    /// </summary>
    private readonly List<ColumnObj> _unIncreColumnList = new List<ColumnObj>();

    public List<ColumnObj> UnIncreColumnList
    {
        get { return _unIncreColumnList; }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="tableSchema">表对象</param>
    /// <param name="prefix">前缀名</param>
    /// <param name="suffix">后缀名</param>
    public TableObj(TableSchema tableSchema, string prefix = null, string suffix = null)
    {
        _tableSchema = tableSchema;
        _prefix = prefix;
        _suffix = suffix;
        Init();
    }

    /// <summary>
    /// 初始化
    /// </summary>
    private void Init()
    {
        foreach (ColumnSchema col in _tableSchema.Columns)
        {
            ColumnObj obj = new ColumnObj(col);

            _columnList.Add(obj);

            if (obj.IsIncremented)
            {
                _increColumnList.Add(obj);
            }
            else
            {
                _unIncreColumnList.Add(obj);
            }

            if (obj.IsPk)
            {
                _pkColumnList.Add(obj);
            }
            else
            {
                _unPkcolumnList.Add(obj);
            }
        }

        _className = GeneHelper.GetClassNmByTbNm(_tableSchema.Name, _prefix, _suffix);
        _orginName = _tableSchema.Name;
    }

    private string _className;

    public string ClassName
    {
        get { return _className; }
    }

    private string _orginName;

    public string OrginName
    {
        get { return _orginName; }
    }
}

public class ColumnObj
{
    private readonly ColumnSchema _columnSchema;

    public ColumnObj(ColumnSchema columnSchema)
    {
        _columnSchema = columnSchema;
        Init();
    }

    private void Init()
    {
        IsPk = _columnSchema.IsPrimaryKeyMember;

        object obj = _columnSchema.ExtendedProperties["CS_IsIdentity"].Value;
        if (obj.ToString() != "False")
        {
            IsIncremented = true;
        }

        OrginName = _columnSchema.Name;
        FieldName = GeneHelper.GetFieldNm(_columnSchema.Name);
        PropertyName = GeneHelper.GetPropNm(_columnSchema.Name);
        DataType = GeneHelper.GetJavaType(_columnSchema.DataType, false);
    }

    /// <summary>
    /// 该列是否是主键
    /// </summary>
    public bool IsPk { get; set; }

    /// <summary>
    /// 该列是否自增
    /// </summary>
    public bool IsIncremented { get; set; }

    /// <summary>
    /// 原始的名字
    /// </summary>
    public string OrginName { get; set; }

    /// <summary>
    /// 字段名
    /// </summary>
    public string FieldName { get; set; }

    /// <summary>
    /// 属性名
    /// </summary>
    public string PropertyName { get; set; }

    /// <summary>
    /// 对应到JAVA里的数据类型
    /// </summary>
    public string DataType { get; set; }
}




